package com.example.tang.zhayan.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.tang.zhayan.BaseApp;
import com.example.tang.zhayan.R;
import com.example.tang.zhayan.bean.DbBuyDetail;
import com.example.tang.zhayan.fragment.HomePageFragment;
import com.example.tang.zhayan.fragment.LimitTimeFragment;
import com.example.tang.zhayan.fragment.WorksFragment;
import com.example.tang.zhayan.utils.FragmentUtils;

import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 主要Activity 包括3 个Fragment和一个Activity
 */
@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //fragment集合
    private List<Fragment> fragmentList;
    private FragmentManager manager;
    private FragmentUtils fragmentUtils;
    @ViewInject(R.id.ll_home_text)
    private LinearLayout llText;
    @ViewInject(R.id.ll_home_img)
    private LinearLayout llImage;
    @ViewInject(R.id.ll_home_container)
    private LinearLayout llContainer;
    private FragmentTransaction transaction;
    @ViewInject(R.id.img_home_personally)
    private ImageView imgClick;
    @ViewInject(R.id.tv_home_personnally)
    private TextView txtClick;
    private SharedPreferences preferences;
    private String userName = "";
    private String icon = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        List<DbBuyDetail> all = null;
        try {
            all = BaseApp.getApp().getDb().findAll(DbBuyDetail.class);
        } catch (DbException e) {
            e.printStackTrace();
        }

        //获取本地存储的用户
        preferences = getSharedPreferences("userMsg", Context.MODE_PRIVATE);

        imgClick.setOnClickListener(this);
        txtClick.setOnClickListener(this);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        //初始化数据
        initData();
        fragmentUtils = new FragmentUtils(llContainer, llImage, llText, fragmentList, manager, R.id.fragment_container, this);
    }


    public static MainActivity newInstance() {
        MainActivity fragment = new MainActivity();
        return fragment;
    }

    public List<Fragment> getFragmentList() {
        return fragmentList;
    }

    private void initData() {
        fragmentList = new ArrayList<>();
        fragmentList.add(HomePageFragment.newInstance());
        fragmentList.add(WorksFragment.newInstance());
        fragmentList.add(LimitTimeFragment.newInstance());
        //首先提交第一个界面
        Fragment fragment = fragmentList.get(0);
        transaction.add(R.id.fragment_container, fragment);
        transaction.commit();
    }

    /**
     * 跳转至 登录界面
     */
    public void seekToLogin() {
        Intent intent = new Intent(this, UserLoginActivity.class);
        startActivity(intent);
    }

    /**
     * 判断用户名是否为空 空 跳转至登录界面 否则跳转到用户界面
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_home_personally:
                getUserMsg();
                if (userName.equals("noName")) {
                    seekToLogin();
                } else {
                    seekToUser();
                }
                break;
            case R.id.tv_home_personnally:
                getUserMsg();
                if (userName.equals("noName")) {
                    seekToLogin();
                } else {
                    seekToUser();
                }
                break;
        }
    }

    public void seekToUser() {
        Intent intent = new Intent(this, UserActivity.class);
        intent.putExtra("icon", icon);
        intent.putExtra("userName", userName);
        startActivity(intent);
    }

    // 设置第一次按的时间
    long currentMillions = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {
            //第一次的时间
            if ((System.currentTimeMillis() - currentMillions) > 2000) {
                Toast.makeText(getApplicationContext(), "你要狠心离开我~吗", Toast.LENGTH_SHORT).show();
                currentMillions = System.currentTimeMillis();
            } else {
                this.finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 获取用户的信息
     */
    public void getUserMsg() {
        userName = preferences.getString("userName", "noName");
        icon = preferences.getString("icon", "noIcon");
    }
}
